package com.glsc.ngateway.platform.service.system;

import com.glsc.ngateway.common.api.common.enums.DictConstant;
import com.glsc.ngateway.common.base.domain.mysql.gateway.system.GwContact;
import com.glsc.ngateway.common.api.platform.dto.system.GwContactDto;
import com.glsc.ngateway.common.base.exception.PlatformException;
import com.glsc.ngateway.common.base.repo.mysql.gateway.platform.it.ItApplicationDetailRepo;
import com.glsc.ngateway.common.base.repo.mysql.gateway.platform.it.ItOperationRepo;
import com.glsc.ngateway.common.base.repo.mysql.gateway.platform.it.ItVirtualDeviceRepo;
import com.glsc.ngateway.common.base.repo.mysql.gateway.platform.system.GwContractRepository;
import com.glsc.ngateway.common.base.repo.mysql.gateway.platform.system.SystemVendorContractRelationRepository;
import com.glsc.ngateway.platform.service.MailService;
import com.glsc.ngateway.platform.service.UserService;
import com.glsc.ngateway.platform.service.it.SequenceService;
import com.glsc.ngateway.platform.service.it.ServerService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Optional;

@Service
public class GwContractService {

    Logger logger = LoggerFactory.getLogger(GwContractService.class);
    @Autowired
    private JdbcTemplate jdbcTemplate;
    @Autowired
    private SequenceService sequenceService;
    @Autowired
    private UserService userService;
    @Autowired
    private ItVirtualDeviceRepo itVirtualDeviceRepo;
    @Autowired
    private ItOperationRepo itOperationRepo;
    @Autowired
    private ItApplicationDetailRepo itApplicationDetailRepo;

    @Autowired
    private ServerService serverService;

    @Autowired
    private MailService mailService;

    @Resource
    private GwContractRepository gwContractRepository;

    @Resource
    private SystemVendorContractRelationRepository systemVendorContractRelationRepository;


    @Transactional
    public void addOrUpdate(GwContact systemMember) {
//        SystemVendorLogInfo log = new SystemVendorLogInfo();
//        if (StrUtil.isNotEmpty(systemMember.getVendorCode())){
//            //修改
//            log.setLogFlag(DictConstant.UPDATE);
//        } else {
//            //新增
//            log.setLogFlag(DictConstant.ADD);
//        }
//        BeanUtil.copyProperties(systemMember,log);
        systemMember.setDeleted(DictConstant.NO_DELETE);
        gwContractRepository.save(systemMember);
//        systemVendorLogInfoRepository.save(log);
    }

    public void delete(Integer id) {
        Optional<GwContact> optional = gwContractRepository.findById(id);
        if (optional.isPresent()) {
            GwContact bean = optional.get();
            bean.setDeleted(DictConstant.DELETED);
            gwContractRepository.save(bean);
        } else {
            throw PlatformException.errorWithOutMail("删除记录不存在");
        }
    }

    public Page<GwContact> search(GwContactDto params) {
        Pageable pageable = PageRequest.of(params.getPageNo()- 1, params.getPageSize());
        params.setDeleted(DictConstant.NO_DELETE);
        Page<GwContact> result = gwContractRepository.searchByParam(pageable,params);
//        List<GwContract> gwContracts = result.getContent();
//        if (CollectionUtil.isNotEmpty(gwContracts)){
//
//            for (GwContract gwContract:gwContracts) {
//                //填充项目经理
//                Integer contractId = gwContract.getId();
//                List<SystemVendorContractRelation> systemVendorContractRelations = systemVendorContractRelationRepository.queryAllByContractIdAndContractType(contractId,DictConstant.CONTRACT_VENDOR);
//                gwContract.setMemberManager(systemVendorContractRelations.stream().map(m->m.getMemberAccount()).collect(Collectors.toList()));
//                //填充供应商
////                List<SystemVendorRelation> systemVendorRelation = systemVendorRelationRepository.queryAllByOpId(opId);
////                List<Integer> vendorId = systemVendorRelation.stream().filter(v->v.getVendorId()!= null).map(v-> Integer.valueOf(v.getVendorId())).collect(Collectors.toList());
////                List<String> vendorIdString = systemVendorRelation.stream().map(v-> v.getVendorId()).collect(Collectors.toList());
////                gwContract.setOpSupport(vendorIdString);
//                //填充供应商名称
//                if (gwContract.getOpSupport() != null){
//                    List<SystemVendorInfo> systemVendorInfos = systemVendorInfoRepository.findAllByIdIn(ImmutableList.of(gwContract.getOpSupport()));
//                    if (CollectionUtil.isNotEmpty(systemVendorInfos)){
//                        gwContract.setOpSupportName(systemVendorInfos.get(0).getVendorName());
//                    }
//                }
//
//                //团队
//                List<SystemGroup> groups = systemGroupRepository.queryAllByOpId(opId);
//                gwContract.setOpGroupId(groups.stream().map(g -> g.getGroupId()).collect(Collectors.toList()));
//                //所属部门
//                List<SystemDepartment> departments = systemDepartmentRepository.queryAllByOpId(opId);
//                gwContract.setBusinessDepartment(departments.stream().map(d->d.getDepartmentId()).collect(Collectors.toList()));
//            }
//        }
        return result;
    }

    public GwContact findById(Integer id) {
        return gwContractRepository.queryById(id);
    }
    public List<GwContact> findByIdList(List<Integer> idList){
        return gwContractRepository.findAllByIdIn(idList);
    }
}
